package com.tpyule.tpclass.service.impl;

import com.tpyule.tpclass.dao.CourseDao;
import com.tpyule.tpclass.entity.bean.ErrorResultBean;
import com.tpyule.tpclass.entity.bean.ResultBean;
import com.tpyule.tpclass.entity.bean.SuccessResultBean;
import com.tpyule.tpclass.entity.bo.CourseBo;
import com.tpyule.tpclass.entity.bo.CourseDetailBo;
import com.tpyule.tpclass.entity.bo.CourseManageBo;
import com.tpyule.tpclass.entity.dto.*;
import com.tpyule.tpclass.entity.pojo.Chapter;
import com.tpyule.tpclass.entity.pojo.Course;
import com.tpyule.tpclass.entity.pojo.Section;
import com.tpyule.tpclass.service.ChapterService;
import com.tpyule.tpclass.service.CourseService;
import com.tpyule.tpclass.service.SectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author zzz
 * @version 1.0
 * @date 2020/3/20 19:52
 */
@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    CourseDao courseDao;
    @Autowired
    ChapterService chapterService;
    @Autowired
    SectionService sectionService;

    @Override
    public List<Course> getAllCourse(CourseParamDto courseParamDto) {
        return courseDao.getAllCourse(courseParamDto);
    }

    @Override
    public List<CourseManageBo> getListManageCourse(CourseManageQueryDto dto) {
        if (dto.getPage()!=0&&dto.getPageSize()!=0){
            dto.setStart((dto.getPage() - 1) * dto.getPageSize());
        }
        return courseDao.getListManageCourse(dto);
    }

    @Override
    public int getSizeManageCourse(CourseManageQueryDto dto) {
        return courseDao.getSizeManageCourse(dto);
    }

    @Override
    public List<CourseManageBo> getListAuditCourse(CourseManageQueryDto dto) {
        if (dto.getPage()!=0&&dto.getPageSize()!=0){
            dto.setStart((dto.getPage() - 1) * dto.getPageSize());
        }
        return courseDao.getListAuditCourse(dto);
    }

    @Override
    public int getSizeAuditCourse(CourseManageQueryDto dto) {
        return courseDao.getSizeAuditCourse(dto);
    }

    @Override
    public List<Course> getListCourseByTeacherId(CourseByTId courseByTId) {
        if (courseByTId.getPage()!=0&&courseByTId.getPageSize()!=0){
            courseByTId.setStart((courseByTId.getPage() - 1) * courseByTId.getPageSize());
        }
        return courseDao.getListCourseByTeacherId(courseByTId);
    }

    @Override
    public Course getCourseById(String courseId) {
        return courseDao.getCourseById(courseId);
    }

    @Override
    public String addCourse(Course course) {
        course.setCourseId(UUID.randomUUID().toString().toUpperCase());
        courseDao.addCourse(course);
        return course.getCourseId();
    }

    @Override
    public List<CourseCDto> getCourseCS(String courseId) {
        List<CourseCDto> courseCDtos = new ArrayList<CourseCDto>();
        List<Chapter> chapters = chapterService.getListChapter(courseId);
        // List<Section> sections = sectionService.getListSectionByChapterId(chapterId);
        for (Chapter chapter : chapters) {
            List<Section> sections = sectionService.getListSectionByChapterId(chapter.getChapterId());
            List<CourseSDto> courseSDtos = new ArrayList<CourseSDto>();
            for (Section section : sections) {
                courseSDtos.add(getCourseSDto(section));
            }
            CourseCDto courseCDto = getCourseCDto(chapter);
            courseCDto.setChildren(courseSDtos);
            courseCDtos.add(courseCDto);
        }
        return courseCDtos;
    }

    @Override
    public boolean deleteCourseById(String courseId) {
        int result = courseDao.deleteCourseById(courseId);
        if (result != 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public ResultBean getCourseDetail(String courseId) {
        ResultBean resultBean = null;
        CourseDetailBo courseDetailBo = courseDao.getDetailPage(courseId);
        if (courseDetailBo == null) {
            resultBean = new ErrorResultBean("该课程不存在");
        } else {
            resultBean = new SuccessResultBean<CourseDetailBo>(courseDetailBo,"获取成功！");
        }
        return resultBean;
    }

    @Override
    public List<CourseCDto> getCourseCSByUser(String courseId, String userId) {
        List<CourseCDto> courseCDtos = new ArrayList();
        List<Chapter> chapters = chapterService.getListChapter(courseId);
        // List<Section> sections = sectionService.getListSectionByChapterId(chapterId);
        for (Chapter chapter : chapters) {
            List sections = sectionService.getSectionStudy(chapter.getChapterId(),userId);
            CourseCDto courseCDto = getCourseCDto(chapter);
            courseCDto.setChildren(sections);
            courseCDtos.add(courseCDto);
        }
        return courseCDtos;
    }

    @Override
    public int updateCourse(Course course) {
        return courseDao.updateCourse(course);
    }

    @Override
    public int updateCourseDetail(CourseBo bo) {
        int result = 0;
        Course course = courseDao.getCourseById(bo.getCourseId());
        if(course!=null){
            Course nCo = getCourseByBo(course, bo);
            if (nCo!=null){
                result=courseDao.updateCourse(nCo);
            }
        }
        return result;
    }

    @Override
    public int updateCourseState(CourseStateDto dto) {
        Course course = courseDao.getCourseById(dto.getCourseId());
        if (dto.getReviewState()!=-1){
            course.setReviewState(dto.getReviewState());
        }
        if (dto.getState()!=-1){
            course.setState(dto.getState());
        }
        int result = courseDao.updateCourse(course);
        return result;
    }

    @Override
    public int getSizeCourse(CourseParamDto courseParamDto) {
        return courseDao.getSizeCourse(courseParamDto);
    }

    @Override
    public Course getCourseByQId(String questionId) {
        return courseDao.getCourseByQId(questionId);
    }

    @Override
    public List<Course> getCourseToShow(String userId) {
        return courseDao.getCourseToShow(userId);
    }

    @Override
    public CourseBo getCourseBoById(String courseId) {
        return courseDao.getCourseBoById(courseId);
    }

    @Override
    public void addNumber(String courseId) {
        courseDao.addNumber(courseId);
    }

    public CourseCDto getCourseCDto(Chapter chapter) {
        CourseCDto courseCDto = new CourseCDto();
        courseCDto.setChapterId(chapter.getChapterId());
        courseCDto.setCourseId(chapter.getCourseId());
        courseCDto.setName(chapter.getName());
        courseCDto.setIntroduction(chapter.getIntroduction());
        courseCDto.setSort(chapter.getSort());
        courseCDto.setDate(chapter.getDate());
        courseCDto.setRemove(chapter.getRemove());
        return courseCDto;
    }

    public CourseSDto getCourseSDto(Section section) {
        CourseSDto courseSDto = new CourseSDto();
        courseSDto.setSectionId(section.getSectionId());
        courseSDto.setChapterId(section.getChapterId());
        courseSDto.setName(section.getName());
        courseSDto.setFileName(section.getFileName());
        courseSDto.setFile(section.getFile());
        courseSDto.setSort(section.getSort());
        courseSDto.setVideoUrl(section.getVideoUrl());
        courseSDto.setVideoSecond(section.getVideoSecond());
        courseSDto.setDate(section.getDate());
        courseSDto.setRemove(section.getRemove());
        return courseSDto;
    }

    public Course getCourseByBo(Course course, CourseBo bo){
        course.setSortId(bo.getSortId()+"");
        course.setTitle(bo.getTitle());
        course.setImageUrl(bo.getImageUrl());
        course.setIntroduction(bo.getIntroduction());
        course.setNotice(bo.getNotice());
        course.setDifficulty(bo.getDifficulty());
        course.setTeacherNotice(bo.getTeacherNotice());
        course.setHotWord(bo.getHotWord());
        return course;
    }
}
